package com.xy.novelmanagement.controller;

import com.xy.model.AdminUser;
import com.xy.model.RestfulResult;
import com.xy.novelmanagement.service.ILoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RequestMapping("/login")
@Slf4j
@RestController
public class LoginController {
		@Autowired
		private ILoginService loginService;
			@PostMapping("/")
		public RestfulResult login(HttpServletRequest request, HttpServletResponse response,@ModelAttribute AdminUser user){
					log.info(user.toString());
					//1:登录成功 2：密码错误：3：用户不存在 4:登录错误
					int res = loginService.login(user);
					if(res==1){
							HttpSession session = request.getSession();
							user = loginService.getAdminUser(user.getUsername());
							session.setAttribute("adminUser",user);
							session.setMaxInactiveInterval(3*60);//设置登录过期时间
					}
					if(res==4){
							return RestfulResult.failed("500","登录出错！请与管理员联系");
					}
					return RestfulResult.success(res);
			}
}
